﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace visualizadordicom.Aux_Forms
{
    public partial class resultshomogeneidade : Form
    {

        double[,] tablesnr;
        double[,] tablempv;
        double[,] tablestd;
        int nx, ny;
        string file;

        //the average values of the std, snr and mpv will be calculated over a hole area and by the average of all the small rois calculated.
        double std, snr, mpv;
        double meansnr, meanstd, meanmpv;

        /// <summary>
        /// This will be the primary constructor of this form.
        /// </summary>
        /// <param name="x">
        /// Gets the snr data as a vector.
        /// </param>
        /// <param name="y">
        /// Gets the mpv data as a vector.
        /// </param>
        /// <param name="z">
        /// Gets the std data as a vector.
        /// </param>
        /// <param name="snr">
        /// It's the snr average value for the entire area.
        /// </param>
        /// <param name="mpv">
        /// It's the mpv average value for the entire area.
        /// </param>
        /// <param name="std">
        /// It's the std average value for the entire area.
        /// </param>
        /// <param name="nx">
        /// It's the number of rois calculated over a row.
        /// </param>
        /// <param name="ny">
        /// It's the number of rois calculated over a column.
        /// </param>
        public resultshomogeneidade(double[] x, double[] y, double[] z,  double snr, double mpv, double std, int nx, int ny)
        {
            InitializeComponent();

            this.ny = ny;
            this.nx = nx;
            //Atributes the desired values to the local variables and round it to improve the formating.
            this.mpv = Math.Round(mpv, 2);
            this.snr = Math.Round(snr, 2);
            this.std = Math.Round(std, 2);
            this.trackBar1.Value = 150;
            this.textBox4.Text = ((double)trackBar1.Value/10).ToString();

            this.file = null;


            //Allocates memory to the local variables that will store the snr, mpv and std values.
            tablesnr = new double[nx, ny];
            tablempv = new double[nx, ny];
            tablestd = new double[nx, ny];

            //Converts the snr, mpv and std data from a vector to a table.
            for (int i = 0; i < nx; i++)
                for (int j = 0; j < ny; j++)
                {
                    tablesnr[i, j] = Math.Round( x[i + j * nx], 2);
                    tablempv[i, j] = Math.Round( y[i + j * nx], 2);
                    tablestd[i, j] = Math.Round( z[i + j * nx], 2);
                }


            meansnr = 0;
            meanstd = 0;
            meanmpv = 0;
            //The mean values of the snr, mpv and std will be required to calculate the relative snr, mpv and std.
            for (int i = 0; i < ny; i++)
                for (int j = 0; j < nx; j++)
                {
                    meansnr += tablesnr[j, i];
                    meanstd += tablestd[j, i];
                    meanmpv += tablempv[j, i];
                }

            meansnr /= (nx * ny);
            meanstd /= (nx * ny);
            meanmpv /= (nx * ny);

            meansnr = Math.Round(meansnr, 2);
            meanstd = Math.Round(meanstd, 2);
            meanmpv = Math.Round(meanmpv, 2);

            showsnr();
            textBox1.Text = this.snr.ToString();
            textBox2.Text = this.mpv.ToString();
            textBox3.Text = this.std.ToString();

        }


        public resultshomogeneidade(double[] x, double[] y, double[] z, double snr, double mpv, double std, int nx, int ny, string file)
        {
            InitializeComponent();

            this.ny = ny;
            this.nx = nx;
            //Atributes the desired values to the local variables and round it to improve the formating.
            this.mpv = Math.Round(mpv, 2);
            this.snr = Math.Round(snr, 2);
            this.std = Math.Round(std, 2);

            this.file = file;

            //Allocates memory to the local variables that will store the snr, mpv and std values.
            tablesnr = new double[nx, ny];
            tablempv = new double[nx, ny];
            tablestd = new double[nx, ny];

            //Converts the snr, mpv and std data from a vector to a table.
            for (int i = 0; i < nx; i++)
                for (int j = 0; j < ny; j++)
                {
                    tablesnr[i, j] = Math.Round(x[i + j * nx], 2);
                    tablempv[i, j] = Math.Round(y[i + j * nx], 2);
                    tablestd[i, j] = Math.Round(z[i + j * nx], 2);
                }


            meansnr = 0;
            meanstd = 0;
            meanmpv = 0;
            //The mean values of the snr, mpv and std will be required to calculate the relative snr, mpv and std.
            for (int i = 0; i < ny; i++)
                for (int j = 0; j < nx; j++)
                {
                    meansnr += tablesnr[j, i];
                    meanstd += tablestd[j, i];
                    meanmpv += tablempv[j, i];
                }

            meansnr /= (nx * ny);
            meanstd /= (nx * ny);
            meanmpv /= (nx * ny);

            meansnr = Math.Round(meansnr, 2);
            meanstd = Math.Round(meanstd, 2);
            meanmpv = Math.Round(meanmpv, 2);

            showsnr();
            textBox1.Text = this.snr.ToString();
            textBox2.Text = this.mpv.ToString();
            textBox3.Text = this.std.ToString();

        }


        /// <summary>
        /// This function shows the snr integral values into the datagridview.
        /// </summary>
        public void showsnr() 
        {
            DataTable table = new DataTable("Mapa de homogeneidade");

            this.label4.Text = "Modo: SNR integral";

            // table.Columns.Add("Posição");
            for (int i = 0; i < nx; i++)
            {
                table.Columns.Add((i + 1).ToString());
            }

            for (int i = 0; i < ny; i++)
            {
                table.Rows.Add();
            }

            for (int i = 0; i < ny; i++)
            {
                for (int j = 0; j < nx; j++)
                {
                    table.Rows[i][j] = tablesnr[j, i];
                }

            }

            dataGridView1.DataSource = table;

            dataGridView1.Update();

        }

        /// <summary>
        /// This function shows the snr relative values compared with the total area values into the datagridview.
        /// </summary>
        public void showsnrrel()
        {
            DataTable table = new DataTable("Mapa de homogeneidade");

            this.label4.Text = "Modo: SNR relativo a área total";


            // table.Columns.Add("Posição");
            for (int i = 0; i < nx; i++)
            {
                table.Columns.Add((i + 1).ToString());
            }

            for (int i = 0; i < ny; i++)
            {
                table.Rows.Add();
            }

            for (int i = 0; i < ny; i++)
            {
                for (int j = 0; j < nx; j++)
                {
                    table.Rows[i][j] = Math.Round( 100 * tablesnr[j, i] / snr, 2);
                }

            }

            dataGridView1.DataSource = table;

            paintdatagridview();

        }

        /// <summary>
        /// This function shows the snr relative values compared the average values of the rois into the datagridview.
        /// </summary>
        public void showsnrmean()
        {
            DataTable table = new DataTable("Mapa de homogeneidade");

            this.label4.Text = "Modo: SNR relativo a media dos rois";


            // table.Columns.Add("Posição");
            for (int i = 0; i < nx; i++)
            {
                table.Columns.Add((i + 1).ToString());
            }

            for (int i = 0; i < ny; i++)
            {
                table.Rows.Add();
            }

            for (int i = 0; i < ny; i++)
            {
                for (int j = 0; j < nx; j++)
                {
                    table.Rows[i][j] = Math.Round(100 * tablesnr[j, i] / meansnr, 2);
                }

            }

            dataGridView1.DataSource = table;

            paintdatagridview();      

        }

        /// <summary>
        ///  This function shows the std integral values into the datagridview.
        /// </summary>
        public void showstd()
        {
            DataTable table = new DataTable("Mapa de homogeneidade");

            this.label4.Text = "Modo: STD integral";



            // table.Columns.Add("Posição");
            for (int i = 0; i < nx; i++)
            {
                table.Columns.Add((i + 1).ToString());
            }

            for (int i = 0; i < ny; i++)
            {
                table.Rows.Add();
            }

            for (int i = 0; i < ny; i++)
            {
                for (int j = 0; j < nx; j++)
                {
                    table.Rows[i][j] = tablestd[j, i];
                }

            }

            dataGridView1.DataSource = table;
            dataGridView1.Update();

        }

        /// <summary>
        ///  This function shows the std relative values compared with the total area values into the datagridview.
        /// </summary>
        public void showstdrel()
        {
            DataTable table = new DataTable("Mapa de homogeneidade");

            this.label4.Text = "Modo: STD relativo a área total";

            // table.Columns.Add("Posição");
            for (int i = 0; i < nx; i++)
            {
                table.Columns.Add((i + 1).ToString());
            }

            for (int i = 0; i < ny; i++)
            {
                table.Rows.Add();
            }

            for (int i = 0; i < ny; i++)
            {
                for (int j = 0; j < nx; j++)
                {
                    table.Rows[i][j] = Math.Round( 100 * tablestd[j, i] / std, 2);
                }

            }

            dataGridView1.DataSource = table;

            paintdatagridview();
        }

        /// <summary>
        /// This function shows the std relative values compared the average values of the rois into the datagridview.
        /// </summary>
        public void showstdmeanroi()
        {
            DataTable table = new DataTable("Mapa de homogeneidade");

            this.label4.Text = "Modo: STD relativo a média dos rois";


            // table.Columns.Add("Posição");
            for (int i = 0; i < nx; i++)
            {
                table.Columns.Add((i + 1).ToString());
            }

            for (int i = 0; i < ny; i++)
            {
                table.Rows.Add();
            }


            for (int i = 0; i < ny; i++)
            {
                for (int j = 0; j < nx; j++)
                {
                    table.Rows[i][j] = Math.Round(100 * tablestd[j, i] / meanstd, 2);
                }

            }

            dataGridView1.DataSource = table;

            paintdatagridview();

        }

        /// <summary>
        /// This function shows the mpv integral values into the datagridview.
        /// </summary>
        public void showmpv()
        {
            DataTable table = new DataTable("Mapa de homogeneidade");

            this.label4.Text = "Modo: MPV integral";


            // table.Columns.Add("Posição");
            for (int i = 0; i < nx; i++)
            {
                table.Columns.Add((i + 1).ToString());
            }

            for (int i = 0; i < ny; i++)
            {
                table.Rows.Add();
            }

            for (int i = 0; i < ny; i++)
            {
                for (int j = 0; j < nx; j++)
                {
                    table.Rows[i][j] = tablempv[j, i];
                }

            }

            dataGridView1.DataSource = table;
            dataGridView1.Update();

        }

        /// <summary>
        /// This function shows the mpv relative values compared with the total area values into the datagridview.
        /// </summary>
        public void showmpvrel()
        {
            DataTable table = new DataTable("Mapa de homogeneidade");

            this.label4.Text = "Modo: MPV relativo a área total";


            // table.Columns.Add("Posição");
            for (int i = 0; i < nx; i++)
            {
                table.Columns.Add((i + 1).ToString());
            }

            for (int i = 0; i < ny; i++)
            {
                table.Rows.Add();
            }

            for (int i = 0; i < ny; i++)
            {
                for (int j = 0; j < nx; j++)
                {
                    table.Rows[i][j] = Math.Round( 100 * tablempv[j, i] / mpv, 2);
                }

            }

            dataGridView1.DataSource = table;

            paintdatagridview();

        }

        /// <summary>
        /// This function shows the mpv relative values compared the average values of the rois into the datagridview.
        /// </summary>
        public void showmpvmeanroi()
        {
            DataTable table = new DataTable("Mapa de homogeneidade");

            this.label4.Text = "Modo: MPV relativo a média dos rois";

            // table.Columns.Add("Posição");
            for (int i = 0; i < nx; i++)
            {
                table.Columns.Add((i + 1).ToString());
            }

            for (int i = 0; i < ny; i++)
            {
                table.Rows.Add();
            }

            for (int i = 0; i < ny; i++)
            {
                for (int j = 0; j < nx; j++)
                {
                    table.Rows[i][j] = Math.Round(100 * tablempv[j, i] / meanmpv, 2);
                }

            }

            dataGridView1.DataSource = table;

            paintdatagridview();

        }

        /// <summary>
     /// This fuction will close the form.
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Radio button snr is changed, the desired table will be shown based on the desired type of mode and average.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton6.Checked)
            {
                if (radioButton7.Checked)
                {
                    showsnr();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else 
                {
                    showsnr();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }
            else 
            {
                if (radioButton7.Checked)
                {
                    showsnrrel();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else
                {
                    showsnrmean();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }
        }

        /// <summary>
        ///  Radio button mpv is changed, the desired table will be shown based on the desired type of mode and average.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton6.Checked)
            {
                if (radioButton7.Checked)
                {
                    showmpv();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else
                {
                    showmpv();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }
            else
            {
                if (radioButton7.Checked)
                {
                    showmpvrel();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else
                {
                    showmpvmeanroi();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }
        }

        /// <summary>
        ///  Radio button std is changed, the desired table will be shown based on the desired type of mode and average.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton6.Checked)
            {
                if (radioButton7.Checked)
                {
                    showstd();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else
                {
                    showstd();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }
            else
            {
                if (radioButton7.Checked)
                {
                    showstdrel();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else
                {
                    showstdmeanroi();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }

        }

        /// <summary>
        ///  Radio button integral mode is changed, the desired table will be shown based on the desired type of calculous and average.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked) 
            {
                if (radioButton7.Checked)
                {
                    showsnr();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else 
                {
                    showsnr();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }
            else if (radioButton2.Checked)
            {
                if (radioButton7.Checked)
                {
                    showmpv();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else
                {
                    showmpv();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }
            else 
            {
                if (radioButton7.Checked)
                {
                    showstd();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else
                {
                    showstd();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }
        }

        /// <summary>
        /// Radio button relative mode is changed, the desired table will be shown based on the desired type of calculous and average.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                if (radioButton7.Checked)
                {
                    showsnrrel();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else
                {
                    showsnrmean();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }
            else if (radioButton2.Checked)
            {
                if (radioButton7.Checked)
                {
                    showmpvrel();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else
                {
                    showmpvmeanroi();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }
            else
            {
                if (radioButton7.Checked)
                {
                    showstdrel();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else
                {
                    showstdmeanroi();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }
        }

        /// <summary>
        /// Radio button full area average is changed, the desired table will be shown based on the desired type of calculous and mode.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton7_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                if (radioButton6.Checked)
                {
                    showsnr();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else
                {
                    showsnrrel();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
            }
            else if (radioButton2.Checked)
            {
                if (radioButton6.Checked)
                {
                    showmpv();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else
                {
                    showmpvrel();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
            }
            else
            {
                if (radioButton6.Checked)
                {
                    showstd();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
                else
                {
                    showstdrel();
                    textBox1.Text = snr.ToString();
                    textBox2.Text = mpv.ToString();
                    textBox3.Text = std.ToString();
                }
            }
        }

        /// <summary>
        /// When the Radio button full area average is changed, the desired table will be shown based on the desired type of calculous and mode.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                if (radioButton6.Checked)
                {
                    showsnr();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
                else
                {
                    showsnrmean();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }
            else if (radioButton2.Checked)
            {
                if (radioButton6.Checked)
                {
                    showmpv();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
                else
                {
                    showmpvmeanroi();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }
            else
            {
                if (radioButton6.Checked)
                {
                    showstd();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
                else
                {
                    showstdmeanroi();
                    textBox1.Text = meansnr.ToString();
                    textBox2.Text = meanmpv.ToString();
                    textBox3.Text = meanstd.ToString();
                }
            }
        }


        /// <summary>
        /// Saves the results of the tests
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            
            //If the proper test file is selected a new windows will be displayed showing the test results, the file string will be sent to this window and the desired informations about the test will be saved.
            if (file != null)
            {
                saveresults save = new saveresults(file, meansnr, snr, meanmpv, mpv, meanstd, std);
                save.Show();
            }
            else 
            {
                //If there is no file selected the selection file window will be displayed and this function will be restarted (to check is the file is selected otherwise this process will restart once again).
                SARD.Aux_Forms.local showoptions = new SARD.Aux_Forms.local();
                showoptions.ShowDialog();

                file = showoptions.getfile();
                button1_Click(sender, e);
            }
        }


        /// <summary>
        /// This function is used to select the proper file before calling the save method, it is not totally necessary, but desired.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            SARD.Aux_Forms.local showoptions = new SARD.Aux_Forms.local();
            showoptions.ShowDialog();


            if (showoptions.getfile() != null)
                file = showoptions.getfile();


        }

        /// <summary>
        /// Creates an image based on the information acquired over the test.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            DataTable table = (DataTable)dataGridView1.DataSource;

            Bitmap bmp = new Bitmap(this.dataGridView1.ColumnCount,this.dataGridView1.RowCount, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            System.Drawing.Imaging.BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, this.dataGridView1.ColumnCount, this.dataGridView1.RowCount), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);

            unsafe
            {
                int pixelSize = 3;
                int i, j, j1;

                for (i = 0; i < bmd.Height; ++i)
                {
                    byte* row = (byte*)bmd.Scan0 + (i * bmd.Stride);

                    for (j = 0; j < bmd.Width; ++j)
                    {
                        j1 = j * pixelSize;
                        if (Convert.ToDouble(table.Rows[i][j]) < 100 - (double)trackBar1.Value / 10 || Convert.ToDouble(table.Rows[i][j]) > 100 + (double)trackBar1.Value / 10)
                        {
                            row[j1] = 0;            // blue
                            row[j1 + 1] = 0;            // Green
                            row[j1 + 2] = 255;            // red
                        }
                        else
                        {
                            row[j1] = 0;          
                            row[j1 + 1] = 0;           
                            row[j1 + 2] = 0;            

                        }

                    }
                }
            } // end unsafe      
            bmp.UnlockBits(bmd);
            visualizadordicom.Aux_Forms.showhomogeneityimage image = new showhomogeneityimage(bmp);
            image.Show();


        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            this.textBox4.Text = ((double)this.trackBar1.Value / 10).ToString();

            if (radioButton5.Checked)
            {
                
                paintdatagridview();
            }
        }

        private void paintdatagridview() 
        {
            DataTable table = (DataTable)dataGridView1.DataSource;

                this.textBox4.Text = ((double)trackBar1.Value / 10).ToString();

                for (int i = 0; i < this.dataGridView1.RowCount; i++)
                    for (int j = 0; j < this.dataGridView1.ColumnCount; j++)
                    {
                        if (Convert.ToDouble(table.Rows[i][j]) < 100 - (double)trackBar1.Value / 10 || Convert.ToDouble(table.Rows[i][j]) > 100 + (double)trackBar1.Value / 10)
                            dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
                        else
                            dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.White;
                    }


                dataGridView1.Update();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (Convert.ToDouble(this.textBox4.Text.ToString()) > 0)
            {
                trackBar1.Value = Convert.ToInt32(Convert.ToDouble(this.textBox4.Text.ToString()) * 10);
                if (radioButton5.Checked)
                {

                    paintdatagridview();
                }
            }
        }


    }
}
